import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/apartment/unlock_permission/add_permission/index.dart";
import "package:ct_pa_majordomo/pages/device/lock/api.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";



class PermissionList extends StatefulWidget {
  final PermissionType type;
  final int roomId;
  final List renter;
  const PermissionList({super.key, required this.type, required this.roomId, required this.renter});

  @override
  State<StatefulWidget> createState() => _PermissionListState();

}

class _PermissionListState extends State<PermissionList> {

  List _list = [];


  void _getLocks() async {
    EasyLoading.show();
    Map resData = await lockApi.lockByRoomList(widget.roomId);
    EasyLoading.dismiss();
    setState(() {
      _list = resData["data"] ?? [];
    });
  }


  String _title(PermissionType type) {
    switch (type) {
      case PermissionType.fingerprint:
        return "指纹";
      case PermissionType.icCard:
        return "IC卡";
    }
  }

  Widget _roundBtn() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      decoration: BoxDecoration(
          color: ThemeVars.colorPrimary,
          borderRadius: BorderRadius.circular(16),
          boxShadow: [BoxShadow(color: ThemeVars.colorPrimaryLight, spreadRadius: 5, blurRadius: 12)]
      ),
      child: Text("添加${_title(widget.type)}", style: TextStyle(fontSize: 14, color: Colors.white),),
    );
  }

  Future<void> _refresh() async {
    await Future.delayed(Duration(seconds: 2));
  }

  @override
  void initState() {
    super.initState();
    _getLocks();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: _title(widget.type),
      child: RefreshIndicator(
        onRefresh: _refresh,
        child: ListView.separated(
          padding: EdgeInsets.all(ThemeVars.paddingPage),
          itemBuilder: (context, index) {
            return MenuBlock(
              items: [
                MenuBlockItem(
                  text: _list[index]["lockName"],
                  showArrow: false,
                  prefix: Container(
                    padding: EdgeInsets.all(6),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(100)),
                      border: Border.all(color: ThemeVars.colorPrimaryLight, width: 2)
                    ),
                    child: Icon(Icons.door_back_door_outlined, color: ThemeVars.colorPrimary,),
                  ),
                  suffix: _roundBtn(),
                  onTap: () {
                    Map arguments = {
                      "type": widget.type,
                      "renter": widget.renter,
                      "lockInfo": _list[index],
                    };
                    Stacker.pushNamed(Keys.APARTMENT_UNLOCK_PERMISSION_ADD, arguments: arguments);
                  }
                ),
              ]
            );
          },
          separatorBuilder: (context, index) => const SizedBox(height: 12,),
          itemCount: _list.length,
        ),
      )
    );
  }

}